package com.hp.ssmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hp.ssmp.dao.AddressMapper;
import com.hp.ssmp.entity.TAddress;
import com.hp.ssmp.entity.TUser;
import com.hp.ssmp.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("addressService")
@Transactional
public class AddressServiceImpl extends ServiceImpl<AddressMapper, TAddress> implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    public Page<TAddress> getAddressPage(Integer currentPage, Integer pageSize, Long userid) {
        Page<TAddress> page = new Page<TAddress>(currentPage, pageSize);
        QueryWrapper<TAddress> wrapper = new QueryWrapper<TAddress>();
        if (null != userid && !"".equals(userid)) {
            wrapper.eq("userid", userid);
        }
        return addressMapper.selectPage(page, wrapper);
    }

    @Override
    public List<TAddress> getAddressList(Long userid) {
        QueryWrapper<TAddress> wrapper = new QueryWrapper<TAddress>();
        if (null != userid && !"".equals(userid)) {
            wrapper.eq("userid", userid);
        }
        return addressMapper.selectList(wrapper);
    }


    @Override
    public Boolean updDefault(Long addressid, Long userid) {
        try {
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("userid", userid);
            updateWrapper.set("isdefault", "2");
            addressMapper.update(null, updateWrapper);

            TAddress tAddress = new TAddress();
            tAddress.setAddressid(addressid);
            tAddress.setIsdefault(1L);
            addressMapper.updateById(tAddress);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
